

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pyamp.ui.input.console &mdash; pyamp v1.2 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="top" title="pyamp v1.2 documentation" href="../../../../index.html" />
    <link rel="up" title="pyamp.ui.input" href="../input.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../input.html" accesskey="U">pyamp.ui.input</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for pyamp.ui.input.console</h1><div class="highlight"><pre>
<span class="c"># Copyright 2012 Brett Ponsler</span>
<span class="c"># This file is part of pyamp.</span>
<span class="c">#</span>
<span class="c"># pyamp is free software: you can redistribute it and/or modify</span>
<span class="c"># it under the terms of the GNU General Public License as published by</span>
<span class="c"># the Free Software Foundation, either version 3 of the License, or</span>
<span class="c"># (at your option) any later version.</span>
<span class="c">#</span>
<span class="c"># pyamp is distributed in the hope that it will be useful,</span>
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c"># GNU General Public License for more details.</span>
<span class="c">#</span>
<span class="c"># You should have received a copy of the GNU General Public License</span>
<span class="c"># along with pyamp.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="sd">&#39;&#39;&#39;The console module provides the ability to retrieve input from the user</span>
<span class="sd">through the console.</span>

<span class="sd">&#39;&#39;&#39;</span>
<span class="kn">from</span> <span class="nn">fcntl</span> <span class="kn">import</span> <span class="n">ioctl</span>
<span class="kn">from</span> <span class="nn">select</span> <span class="kn">import</span> <span class="n">select</span>
<span class="kn">from</span> <span class="nn">struct</span> <span class="kn">import</span> <span class="n">unpack</span>
<span class="kn">from</span> <span class="nn">tty</span> <span class="kn">import</span> <span class="n">setcbreak</span>
<span class="kn">from</span> <span class="nn">sys</span> <span class="kn">import</span> <span class="n">stdin</span><span class="p">,</span> <span class="n">stdout</span><span class="p">,</span> <span class="n">stderr</span>
<span class="kn">from</span> <span class="nn">termios</span> <span class="kn">import</span> <span class="n">tcgetattr</span><span class="p">,</span> <span class="n">tcsetattr</span><span class="p">,</span> <span class="n">TCSADRAIN</span><span class="p">,</span> <span class="n">TIOCGWINSZ</span>

<span class="kn">from</span> <span class="nn">pyamp.util</span> <span class="kn">import</span> <span class="n">getStackTrace</span>
<span class="kn">from</span> <span class="nn">pyamp.processes.threading</span> <span class="kn">import</span> <span class="n">Thread</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;ConsoleInput&quot;</span><span class="p">]</span>


<div class="viewcode-block" id="ConsoleInput"><a class="viewcode-back" href="../../../../_generated/pyamp_ui_input_console.html#pyamp.ui.input.console.ConsoleInput">[docs]</a><span class="k">class</span> <span class="nc">ConsoleInput</span><span class="p">(</span><span class="n">Thread</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The ConsoleInput class provides the ability to receive input from</span>
<span class="sd">    a user through the console. This class runs as a separate thread which</span>
<span class="sd">    monitors the console for input and notifies a callback method in the event</span>
<span class="sd">    that keyboard input is received.</span>

<span class="sd">    The callback method must take one argument which is the character that was</span>
<span class="sd">    read as input. This class also provides the ability to register a callback</span>
<span class="sd">    function to be called in the event that an exception occurs during the</span>
<span class="sd">    processing of the thread.</span>

<span class="sd">    This class implements the :class:`pyamp.processes.Thread`, and the</span>
<span class="sd">    :class:`amp.logging.Loggable` interfaces.</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">conditionFn</span><span class="o">=</span><span class="k">lambda</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
                 <span class="n">exceptionFn</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">logger</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * callback -- The function called when input is received</span>
<span class="sd">        * conditionFn -- The function used to determine whether we should</span>
<span class="sd">                         continue waiting for input</span>
<span class="sd">        * exceptionFn -- The functionc called when an exception is encountered</span>
<span class="sd">        * logger -- The logger object</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">Thread</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">conditionFn</span><span class="p">,</span> <span class="n">logger</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">__callback</span> <span class="o">=</span> <span class="n">callback</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__exceptionFn</span> <span class="o">=</span> <span class="n">exceptionFn</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__shouldRun</span> <span class="o">=</span> <span class="bp">False</span>

<div class="viewcode-block" id="ConsoleInput.onStart"><a class="viewcode-back" href="../../../../_generated/pyamp_ui_input_console.html#pyamp.ui.input.console.ConsoleInput.onStart">[docs]</a>    <span class="k">def</span> <span class="nf">onStart</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Called when the ConsoleInput thread is started.&#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__oldSettings</span> <span class="o">=</span> <span class="n">tcgetattr</span><span class="p">(</span><span class="n">stdin</span><span class="p">)</span>
        <span class="n">setcbreak</span><span class="p">(</span><span class="n">stdin</span><span class="o">.</span><span class="n">fileno</span><span class="p">())</span>
</div>
<div class="viewcode-block" id="ConsoleInput.onCycle"><a class="viewcode-back" href="../../../../_generated/pyamp_ui_input_console.html#pyamp.ui.input.console.ConsoleInput.onCycle">[docs]</a>    <span class="k">def</span> <span class="nf">onCycle</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">_increment</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Called during each cycle of the Thread.</span>

<span class="sd">        * increment -- The current increment number</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">ConsoleInput</span><span class="o">.</span><span class="n">__isData</span><span class="p">():</span>
                <span class="n">c</span> <span class="o">=</span> <span class="n">stdin</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">__callback</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">onException</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">getStackTrace</span><span class="p">())</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">shutdown</span><span class="p">()</span>
</div>
<div class="viewcode-block" id="ConsoleInput.onShutdown"><a class="viewcode-back" href="../../../../_generated/pyamp_ui_input_console.html#pyamp.ui.input.console.ConsoleInput.onShutdown">[docs]</a>    <span class="k">def</span> <span class="nf">onShutdown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Called when the ConsoleInput Thread is shutdown.&#39;&#39;&#39;</span>
        <span class="n">tcsetattr</span><span class="p">(</span><span class="n">stdin</span><span class="p">,</span> <span class="n">TCSADRAIN</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__oldSettings</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="ConsoleInput.onException"><a class="viewcode-back" href="../../../../_generated/pyamp_ui_input_console.html#pyamp.ui.input.console.ConsoleInput.onException">[docs]</a>    <span class="k">def</span> <span class="nf">onException</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">,</span> <span class="n">traceback</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;This function is called in the event that an exception</span>
<span class="sd">        occurs during the processing of the thread.</span>

<span class="sd">        * e -- The exception</span>
<span class="sd">        * traceback -- The traceback</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__exceptionFn</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__exceptionFn</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">traceback</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">print</span> <span class="o">&gt;&gt;</span> <span class="n">stderr</span><span class="p">,</span> <span class="n">traceback</span>
</div>
    <span class="nd">@classmethod</span>
<div class="viewcode-block" id="ConsoleInput.clearScreen"><a class="viewcode-back" href="../../../../_generated/pyamp_ui_input_console.html#pyamp.ui.input.console.ConsoleInput.clearScreen">[docs]</a>    <span class="k">def</span> <span class="nf">clearScreen</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Clear the console screen.&#39;&#39;&#39;</span>
        <span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\033</span><span class="s">[2J&#39;</span><span class="p">)</span>
        <span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\033</span><span class="s">[H&#39;</span><span class="p">)</span>
        <span class="n">stdout</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
</div>
    <span class="nd">@classmethod</span>
<div class="viewcode-block" id="ConsoleInput.getSize"><a class="viewcode-back" href="../../../../_generated/pyamp_ui_input_console.html#pyamp.ui.input.console.ConsoleInput.getSize">[docs]</a>    <span class="k">def</span> <span class="nf">getSize</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Get the size of the current console screen.&#39;&#39;&#39;</span>
        <span class="n">rows</span><span class="p">,</span> <span class="n">cols</span> <span class="o">=</span> <span class="n">unpack</span><span class="p">(</span><span class="s">&#39;hh&#39;</span><span class="p">,</span> <span class="n">ioctl</span><span class="p">(</span><span class="n">stdout</span><span class="p">,</span> <span class="n">TIOCGWINSZ</span><span class="p">,</span> <span class="s">&#39;1234&#39;</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">rows</span><span class="p">,</span> <span class="n">cols</span>
</div>
    <span class="nd">@classmethod</span>
<div class="viewcode-block" id="ConsoleInput.getRows"><a class="viewcode-back" href="../../../../_generated/pyamp_ui_input_console.html#pyamp.ui.input.console.ConsoleInput.getRows">[docs]</a>    <span class="k">def</span> <span class="nf">getRows</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Get the number of rows for this console screen.&#39;&#39;&#39;</span>
        <span class="n">rows</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">ConsoleInput</span><span class="o">.</span><span class="n">getSize</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">rows</span>
</div>
    <span class="nd">@classmethod</span>
<div class="viewcode-block" id="ConsoleInput.getCols"><a class="viewcode-back" href="../../../../_generated/pyamp_ui_input_console.html#pyamp.ui.input.console.ConsoleInput.getCols">[docs]</a>    <span class="k">def</span> <span class="nf">getCols</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Get the number of columns for this console screen.&#39;&#39;&#39;</span>
        <span class="n">_</span><span class="p">,</span> <span class="n">cols</span> <span class="o">=</span> <span class="n">ConsoleInput</span><span class="o">.</span><span class="n">getSize</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">cols</span>
</div>
    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">__isData</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Determine if there is input available to process.&#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="n">select</span><span class="p">([</span><span class="n">stdin</span><span class="p">],</span> <span class="p">[],</span> <span class="p">[],</span> <span class="mi">0</span><span class="p">)</span> <span class="o">==</span> <span class="p">([</span><span class="n">stdin</span><span class="p">],</span> <span class="p">[],</span> <span class="p">[])</span>

</div>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">printIn</span><span class="p">(</span><span class="n">message</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Test function for printing a line.</span>

<span class="sd">        * message -- The message to print</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">print</span> <span class="n">message</span>

    <span class="n">console</span> <span class="o">=</span> <span class="n">ConsoleInput</span><span class="p">(</span><span class="n">printIn</span><span class="p">)</span>
    <span class="n">console</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../input.html" >pyamp.ui.input</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Brett Ponsler.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
    </div>
  </body>
</html>